package step1_homework4_01;

import java.io.Serializable;
import java.util.Objects;

public class Student implements Serializable {
    private static final long serialVersionUID = 2084828948125390358L;
    private Integer id;                       //成员变量：学生id
    private String  name;                     //成员变量：学生姓名
    private Integer age;                      //成员变量：学生年龄
    //提供无参和有参的构造方法
    public Student() {
    }

    public Student(Integer id, String name, Integer age) throws IdException, AgeException {
        setId(id);
        setName(name);
        setAge(age);
    }

    //提供公有的get和set方法
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) throws IdException {
        if (id > 0){
        this.id = id;}
        else{
            throw new IdException("ID不合理哦！");
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        if (null != name )
        this.name = name;
    }

    public Integer getAge() {

        return age;
    }

    public void setAge(Integer age) throws AgeException {
        if(age >=0 &&age < 150)
        this.age = age;
        else{
            throw new AgeException("年龄不合理哦！");
        }

    }
    //重写equals、hashCode、toString方法
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o;
        if (!Objects.equals(id, student.id)) return false;
        if (!Objects.equals(name, student.name)) return false;
        return Objects.equals(age, student.age);

    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, age);
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
